using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using business = Microsoft.ConnectionEngine.Registers.Health.Patient.Business;
using common = Microsoft.ConnectionEngine.Common;
using dao = Microsoft.ConnectionEngine.Registers.Health.Patient.Common;
using Microsoft.ConnectionEngine.Common.Security;
using facade = Microsoft.ConnectionEngine.Registers.Health.Patient.Facade;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Registers.Health.Patient.Common;

namespace Microsoft.ConnectionEngine.Registers.Health.Patient.Facade
{
    class PatientManager
    {
        #region Update/Insert

        public static void PatientUpdate(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            dao.Patient patient = new dao.Patient();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pat", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pat:UserCredentials", nsmgr);
            HSDPrincipal currentUser = facade.Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode patientNode = xmlMessage.SelectSingleNode("//pat:Patient", nsmgr);
            patient = (dao.Patient)common.Utilities.DeserializeObject(patientNode.OuterXml, typeof(dao.Patient));

            bool status = business.PatientManager.Update(patient, currentUser.HSDIdentity.UserId);

            resultMsgType = (int)MessageTypes.PatientRegisterUpdateResult;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(patient, typeof(dao.Patient)));
        }

        public static void PatientInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            dao.Patient patient = new dao.Patient();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pat", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pat:UserCredentials", nsmgr);
            HSDPrincipal currentUser = facade.Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode patientNode = xmlMessage.SelectSingleNode("//pat:Patient", nsmgr);
            patient = (dao.Patient)common.Utilities.DeserializeObject(patientNode.OuterXml, typeof(dao.Patient));

            bool status = business.PatientManager.Insert(patient, currentUser.HSDIdentity.UserId);

            resultMsgType = (int)MessageTypes.PatientRegisterInsertResult;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(patient, typeof(dao.Patient)));
        }

        #endregion

        #region Locking

        public static void PatientLock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pat", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pat:UserCredentials", nsmgr);
            HSDPrincipal currentUser = facade.Utilities.CreateUser(userCredentials, nsmgr);

            string patientID = xmlMessage.SelectSingleNode("//pat:PatientID", nsmgr).InnerText;

            bool status = business.PatientManager.Lock(patientID, currentUser.HSDIdentity.UserId);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        public static void PatientUnlock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pat", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pat:UserCredentials", nsmgr);
            HSDPrincipal currentUser = facade.Utilities.CreateUser(userCredentials, nsmgr);

            string patientID = xmlMessage.SelectSingleNode("//pat:PatientID", nsmgr).InnerText;

            bool status = business.PatientManager.Unlock(patientID, currentUser.HSDIdentity.UserId);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        public static void PatientUnlockAll(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pat", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pat:UserCredentials", nsmgr);
            HSDPrincipal currentUser = facade.Utilities.CreateUser(userCredentials, nsmgr);

            bool status = business.PatientManager.UnlockAll(currentUser.HSDIdentity.UserId);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }
        #endregion


        #region Search/Select
        public static void PatientSearch(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            SearchRequest searchRequest = new SearchRequest();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pat", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pat:UserCredentials", nsmgr);
            HSDPrincipal currentUser = facade.Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode searchRequestNode = xmlMessage.SelectSingleNode("//pat:SearchRequest", nsmgr);
            searchRequest = (SearchRequest)common.Utilities.DeserializeObject(searchRequestNode.OuterXml, typeof(SearchRequest));

            Patients patient = business.PatientManager.Search(searchRequest, currentUser.HSDIdentity.UserId);

            resultMsgType = (int)MessageTypes.PatientRegisterSearchResult;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(patient, typeof(Patients)));
        }

        public static void PatientSelect(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pat", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pat:UserCredentials", nsmgr);
            HSDPrincipal currentUser = facade.Utilities.CreateUser(userCredentials, nsmgr);

            string patientID = xmlMessage.SelectSingleNode("//pat:PatientID", nsmgr).InnerText;

            dao.Patient patient = business.PatientManager.Select(patientID, currentUser.HSDIdentity.UserId);

            resultMsgType = (int)MessageTypes.PatientRegisterSelectResult;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(patient, typeof(dao.Patient)));
        }


        #endregion








    }
}
